{% set ceph_hosts = (groups['ceph_monitor'] + groups['ceph_osd'] + groups['ceph_radosgw']) | unique %}
{% set controller_hosts = (groups['pacemaker'] +
               groups['haproxy'] +
               groups['memcached'] +
               groups['rabbitmq'] +
               groups['galera'] +
               groups['mongodb'] +
               groups['keystone'] +
               groups['swift_proxy'] +
               groups['swift_storage'] +
               groups['glance'] +
               groups['horizon'] +
               groups['nova_controller'] +
               groups['neutron_controller'] +
               groups['ceilometer_controller'] +
               groups['heat'] +
               groups['trove'] +
               groups['cinder'] +
               groups['murano'] +
               groups['sahara'] +
               groups['barbican'] +
               groups['ironic']) | unique %}

{% set volume_hosts = groups['cinder_volume'] %}

{% set compute_hosts = (groups['nova_compute'] +
               groups['neutron_compute'] +
               groups['ceilometer_compute']) | unique %}

{% set hosts = (ceph_hosts + controller_hosts + volume_hosts + compute_hosts) | unique %}

define command{
        command_name    {{ nagios_cloud_prefix }}check_http_url
        command_line    /usr/lib/nagios/plugins/check_http -H '$HOSTADDRESS$' -I '$HOSTADDRESS$' '$ARG1$' '$ARG2$'
        }

define command{
        command_name    {{ nagios_cloud_prefix }}check_https_url
        command_line    /usr/lib/nagios/plugins/check_http --ssl -H '$HOSTADDRESS$' -I '$HOSTADDRESS$' '$ARG1$' '$ARG2$'
        }

{% macro hostgroup(name, alias, notes, members=[]) -%}
define hostgroup{
    hostgroup_name   {{ nagios_cloud_prefix }}{{ name }}
    alias            {{ nagios_cloud_prefix }}{{ alias }}
    notes            {{ notes }}
{% if (members | count) > 0 %}
    members          {{ ','.join(members) }}
{% endif %}
}
{%- endmacro %}

{% if nagios_monitor_vip_public %}
{% set vips_monitored = nagios_cloud_prefix + 'mgmt-VIP,' + nagios_cloud_prefix + 'public-VIP' %}
{% else %}
{% set vips_monitored = '{{ nagios_cloud_prefix }}mgmt-VIP' %}
{% endif %}

{{ hostgroup('mgmt-VIP', 'mgmt-VIP', 'VIP on the management network') }}
{{ hostgroup('public-VIP', 'public-VIP', 'VIP on the public network') }}

{{ hostgroup('physical hosts', 'physical hosts', 'All physical hosts', hosts) }}

{{ hostgroup('ceph', 'ceph', 'Ceph monitors + OSDs + RadosGW') }}
{{ hostgroup('controller', 'controller', 'Cloud Controller hosts') }}
{{ hostgroup('volume', 'volume', 'OpenStack Volume hosts') }}
{{ hostgroup('compute', 'compute', 'OpenStack Compute hosts') }}
{{ hostgroup('ceph-monitor', 'ceph-monitor', 'Ceph monitors', groups['ceph_monitor']) }}
{{ hostgroup('ceph-radosgw', 'ceph-radosgw', 'Ceph Radosgateway', groups['ceph_radosgw']) }}
{{ hostgroup('pacemaker', 'pacemaker', 'Pacemaker hosts', groups['pacemaker']) }}
{{ hostgroup('haproxy', 'haproxy', 'HAProxy hosts', groups['haproxy']) }}
{{ hostgroup('memcached', 'memcached', 'Memcached hosts', groups['memcached']) }}
{{ hostgroup('rabbitmq', 'rabbitmq', 'RabbitMQ hosts', groups['rabbitmq']) }}
{{ hostgroup('galera', 'galera', 'Galera hosts', groups['galera']) }}
{{ hostgroup('mongodb', 'mongodb', 'MongoDB hosts', groups['mongodb']) }}
{{ hostgroup('keystone', 'keystone', 'Keystone hosts', groups['keystone']) }}
{{ hostgroup('swift-proxy', 'swift-proxy', 'Swift proxy hosts', groups['swift_proxy']) }}
{{ hostgroup('swift-storage', 'swift-storage', 'Swift storage hosts', groups['swift_storage']) }}
{{ hostgroup('ironic', 'ironic', 'OpenStack Ironic hosts', groups['ironic']) }}
{{ hostgroup('glance', 'glance', 'OpenStack Glance hosts', groups['glance']) }}
{{ hostgroup('horizon', 'horizon', 'OpenStack Horizon hosts', groups['horizon']) }}
{{ hostgroup('nova', 'nova', 'OpenStack Nova hosts', groups['nova_controller']) }}
{{ hostgroup('neutron', 'neutron', 'OpenStack Neutron hosts', groups['neutron_controller']) }}
{{ hostgroup('ceilometer', 'ceilometer', 'OpenStack Ceilometer hosts', groups['ceilometer_controller']) }}
{{ hostgroup('heat', 'heat', 'OpenStack Heat hosts', groups['heat']) }}
{{ hostgroup('trove', 'trove', 'OpenStack Trove hosts', groups['trove']) }}
{{ hostgroup('cinder', 'cinder', 'OpenStack Cinder hosts', groups['cinder']) }}
{{ hostgroup('murano', 'murano', 'OpenStack Murano hosts', groups['murano']) }}
{{ hostgroup('sahara', 'sahara', 'OpenStack Sahara hosts', groups['sahara']) }}
{{ hostgroup('barbican', 'barbican', 'OpenStack Barbican hosts', groups['barbican']) }}

define servicegroup{
    servicegroup_name {{ nagios_cloud_prefix }}ceph
    alias {{ nagios_cloud_prefix }}ceph
}

define servicegroup{
    servicegroup_name {{ nagios_cloud_prefix }}OpenStack
    alias {{ nagios_cloud_prefix }}OpenStack
}

define servicegroup{
    servicegroup_name {{ nagios_cloud_prefix }}Infra
    alias {{ nagios_cloud_prefix }}Infra
}

define servicegroup{
    servicegroup_name {{ nagios_cloud_prefix }}general
    alias {{ nagios_cloud_prefix }}general
}

define servicegroup{
    servicegroup_name {{ nagios_cloud_prefix }}hardware
    alias {{ nagios_cloud_prefix }}hardware
}

define host{
    use {{ nagios_host_template }}
    host_name {{ nagios_cloud_prefix }}mgmt-VIP
    alias {{ nagios_cloud_prefix }}mgmt-VIP
    address {{ vip_mgmt }}
    hostgroups {{ nagios_cloud_prefix }}mgmt-VIP
}

{% if nagios_monitor_vip_public %}
define host{
    use {{ nagios_host_template }}
    host_name {{ nagios_cloud_prefix }}public-VIP
    alias {{ nagios_cloud_prefix }}public-VIP
    address {{ vip_public }}
    hostgroups {{ nagios_cloud_prefix }}public-VIP
}
{% endif %}

define service{
    use {{ nagios_service_template }}
    host_name {{ nagios_cloud_prefix }}mgmt-VIP
    service_description PING
    check_command check_ping!100.0,20%!500.0,60%
}

### General checks for all hosts ###
{% for host in hosts %}

{% set hostgroups = [] %}
{% if host in ceph_hosts %}
{% set _= hostgroups.append(nagios_cloud_prefix + 'ceph') %}
{% endif %}
{% if host in controller_hosts %}
{% set _= hostgroups.append(nagios_cloud_prefix + 'controller') %}
{% endif %}
{% if host in volume_hosts %}
{% set _= hostgroups.append(nagios_cloud_prefix + 'volume') %}
{% endif %}
{% if host in compute_hosts %}
{% set _= hostgroups.append(nagios_cloud_prefix + 'compute') %}
{% endif %}

define host{
    use {{ nagios_host_template }}
    host_name {{ host }}
    alias {{ host }}
    address {{ hostvars[host].ip.mgmt }}
    hostgroups {{ ','.join(hostgroups) }}
}

define service{
    use {{ nagios_service_template }}
    host_name {{ host }}
    servicegroups {{ nagios_cloud_prefix }}general
    service_description PING
    check_command check_ping!100.0,20%!500.0,60%
}

define service{
    use {{ nagios_service_template }}
    host_name {{ host }}
    servicegroups {{ nagios_cloud_prefix }}general
    service_description LOAD
    check_command {{ nagios_command_check_nrpe }}!check_load
}

define service{
    use {{ nagios_service_template }}
    host_name {{ host }}
    servicegroups {{ nagios_cloud_prefix }}general
    service_description disk_root
    check_command {{ nagios_command_check_nrpe }}!check_disk_root
}

define service{
    use {{ nagios_service_template }}
    host_name {{ host }}
    servicegroups {{ nagios_cloud_prefix }}general
    service_description disk_var
    check_command {{ nagios_command_check_nrpe }}!check_disk_var
}

define service{
    use {{ nagios_service_template }}
    host_name {{ host }}
    servicegroups {{ nagios_cloud_prefix }}general
    service_description disk_log
    check_command {{ nagios_command_check_nrpe }}!check_disk_log
}

define service{
    use {{ nagios_service_template }}
    host_name {{ host }}
    servicegroups {{ nagios_cloud_prefix }}general
    service_description SSH
    check_command check_ssh
}

{% if hostvars[host].nagios_ipmi | default(nagios_ipmi) %}
define service{
    use {{ nagios_service_template }}
    host_name {{ host }}
    servicegroups {{ nagios_cloud_prefix}}hardware
    service_description sensors
    check_command {{ nagios_command_check_nrpe }}!check_ipmi
}
{% endif %}

{% if hostvars[host].nagios_bond | default(nagios_bond) %}
define service{
    use {{ nagios_service_template }}
    host_name {{ host }}
    servicegroups {{ nagios_cloud_prefix}}hardware
    service_description bonding
    check_command {{ nagios_command_check_nrpe }}!check_bonding
}
{% endif %}

{% if hostvars[host].nagios_mdraid | default(nagios_mdraid) %}
define service{
    use {{ nagios_service_template }}
    host_name {{ host }}
    servicegroups {{ nagios_cloud_prefix}}hardware
    service_description swraid
    check_command {{ nagios_command_check_nrpe }}!check_swraid
}
{% endif %}

{% endfor %}

{% if groups['nova_compute'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}compute
    servicegroups {{ nagios_cloud_prefix }}general
    service_description disk_nova
    check_command {{ nagios_command_check_nrpe }}!check_disk_nova
}
{% endif %}

### CEPH ###

{% if groups['ceph_monitor'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}ceph-monitor
    servicegroups {{ nagios_cloud_prefix }}ceph
    service_description ceph_health
    check_command {{ nagios_command_check_nrpe }}!check_ceph_health
}

define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}ceph-monitor
    servicegroups {{ nagios_cloud_prefix }}ceph
    service_description ceph_df
    check_command {{ nagios_command_check_nrpe }}!check_ceph_df
}
{% endif %}

{% if groups['ceph_radosgw'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}ceph-radosgw,{{ vips_monitored }}
    servicegroups ceph
    service_description radosgw
    check_command {{ radosgw_ssl | ternary('check_https','check_http') }}!-p {{ radosgw_port }}
}
{% endif %}

### INFRA ###

{% if groups['galera'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix}}galera
    servicegroups {{ nagios_cloud_prefix }}general,{{ nagios_cloud_prefix }}Infra
    service_description disk_mysql
    check_command {{ nagios_command_check_nrpe }}!check_disk_mysql
}

define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}galera
    servicegroups {{ nagios_cloud_prefix }}Infra
    service_description galera
    check_command {{ nagios_command_check_nrpe }}!check_galera
}

{% endif %}

{% if groups['mongodb'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}mongodb
    servicegroups {{ nagios_cloud_prefix }}general,{{ nagios_cloud_prefix }}Infra
    service_description disk_mongodb
    check_command {{ nagios_command_check_nrpe }}!check_disk_mongodb
}
{% endif %}


{% if groups['memcached'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}memcached
    servicegroups {{ nagios_cloud_prefix }}Infra
    service_description memcached
    check_command {{ nagios_command_check_nrpe }}!check_memcached
}
{% endif %}

{% if groups['haproxy'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}haproxy
    servicegroups {{ nagios_cloud_prefix }}Infra
    service_description haproxy
    check_command {{ nagios_command_check_nrpe }}!check_haproxy
}
{% endif %}

{% if groups['rabbitmq'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}rabbitmq
    servicegroups {{ nagios_cloud_prefix }}Infra
    service_description rabbitmq
    check_command {{ nagios_command_check_nrpe }}!check_rabbitmq
}
{% endif %}

### OpenStack ###

{% if groups['swift_proxy'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}swift-proxy,{{ vips_monitored }}
    servicegroups {{ nagios_cloud_prefix }}OpenStack
    service_description swift-proxy
    check_command {{ nagios_cloud_prefix }}{{ os_ssl | ternary('check_https_url','check_http_url') }}!-p {{ 8080 }}!-u /healthcheck
}
{% endif %}

{% if groups['keystone'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}keystone,{{ vips_monitored }}
    servicegroups {{ nagios_cloud_prefix }}OpenStack
    service_description keystone-main
    check_command {{ keystone_ssl | ternary('check_https','check_http') }}!-p 5000
}

define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}keystone,{{ nagios_cloud_prefix }}mgmt-VIP
    servicegroups {{ nagios_cloud_prefix }}OpenStack
    service_description keystone-admin
    check_command {{ keystone_ssl | ternary('check_https','check_http') }}!-p 35357
}
{% endif %}

{% if groups['nova_controller'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}nova,{{ vips_monitored }}
    servicegroups {{ nagios_cloud_prefix }}OpenStack
    service_description nova-api
    check_command {{ os_ssl | ternary('check_https','check_http') }}!-p 8774
}
{% endif %}

{% if groups['neutron_controller'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}neutron,{{ vips_monitored }}
    servicegroups {{ nagios_cloud_prefix }}OpenStack
    service_description neutron-api
    check_command {{ os_ssl | ternary('check_https','check_http') }}!-p 9696
}
{% endif %}

{% if groups['cinder'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}cinder,{{ vips_monitored }}
    servicegroups {{ nagios_cloud_prefix }}OpenStack
    service_description cinder-api
    check_command {{ os_ssl | ternary('check_https','check_http') }}!-p 8776
}
{% endif %}

{% if groups['glance'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}glance,{{ vips_monitored }}
    servicegroups {{ nagios_cloud_prefix }}OpenStack
    service_description glance-api
    check_command {{ nagios_cloud_prefix }}{{ os_ssl | ternary('check_https_url','check_http_url') }}!-p 9292!-u /healthcheck
}

define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}glance,{{ nagios_cloud_prefix }}mgmt-VIP
    servicegroups {{ nagios_cloud_prefix }}OpenStack
    service_description glance-registry
    check_command {{ nagios_cloud_prefix }}{{ os_ssl | ternary('check_https_url','check_http_url') }}!-p 9191!-u /healthcheck
}
{% endif %}

{% if groups['ceilometer_controller'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}ceilometer,{{ vips_monitored }}
    servicegroups {{ nagios_cloud_prefix }}OpenStack
    service_description ceilometer-api
    check_command {{ nagios_cloud_prefix }}{{ os_ssl | ternary('check_https_url','check_http_url') }}!-p 8777!-e 401
}
{% endif %}

{% if groups['aodh'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}ceilometer,{{ vips_monitored }}
    servicegroups {{ nagios_cloud_prefix }}OpenStack
    service_description aodh-api
    check_command {{ nagios_cloud_prefix }}{{ os_ssl | ternary('check_https_url','check_http_url') }}!-p 8042!-e 401
}
{% endif %}

{% if groups['heat'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}heat,{{ vips_monitored }}
    servicegroups {{ nagios_cloud_prefix }}OpenStack
    service_description heat-api
    check_command check_http!-p 8004
}
{% endif %}

{% if groups['ironic'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}ironic,{{ vips_monitored }}
    servicegroups {{ nagios_cloud_prefix }}OpenStack
    service_description ironic-api
    check_command check_http!-p 6385
}
{% endif %}

{% if groups['barbican'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}barbican,{{ vips_monitored }}
    servicegroups {{ nagios_cloud_prefix }}OpenStack
    service_description barbican-api
    check_command check_http!-p 9311
}
{% endif %}

{% if groups['trove'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}trove,{{ vips_monitored }}
    servicegroups {{ nagios_cloud_prefix }}OpenStack
    service_description trove-api
    check_command check_http!-p 8779
}
{% endif %}

{% if groups['murano'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}murano,{{ vips_monitored }}
    servicegroups {{ nagios_cloud_prefix }}OpenStack
    service_description murano-api
    check_command {{ os_ssl | ternary('check_https','check_http') }}!-p 8082
}
{% endif %}

{% if groups['sahara'] | count > 0 %}
define service{
    use {{ nagios_service_template }}
    hostgroup_name {{ nagios_cloud_prefix }}sahara,{{ vips_monitored }}
    servicegroups {{ nagios_cloud_prefix }}OpenStack
    service_description sahara-api
    check_command {{ nagios_cloud_prefix }}check_http_url!-p 8386!-e 401
}
{% endif %}
